package com.sneakxy.mybatis.commons.repository.query.domain.criterions;

import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.session.Configuration;

import com.sneakxy.mybatis.commons.dialect.Dialect;
import com.sneakxy.mybatis.commons.orm.mapping.MybatisColumn;

/**
 * @author 潜行的青衣
 */
public class CompareCriterion extends AbstractCriterion {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7107993974980360314L;
	
	private String symbol;
	
	private boolean ignoreCase;
	
	/**
	 * @param domainClass
	 * @param property
	 * @param value
	 */
	public CompareCriterion(Class<?> domainClass, String property, Object value, String symbol) {
		this(domainClass, property, value, symbol, false);
	}
	
	public CompareCriterion(Class<?> domainClass, String property, Object value, String symbol, boolean ignoreCase) {
		super(domainClass, property, value);
		this.symbol = symbol;
		this.ignoreCase = ignoreCase;
	}
	/* (non-Javadoc)
	 * @see com.sneakxy.mybatis.commons.repository.query.domain.Criterion#toSql(java.lang.String)
	 */
	@Override
	public SqlNode toSql(Configuration config, String operator, String parameterName) {
		Dialect dialect = this.getDialect(config);
		List<MybatisColumn> columns = this.getColumns(config.getTypeHandlerRegistry(), parameterName);
		MybatisColumn column = columns.get(0);
		String columnName = this.isIgnoreCase() ? dialect.upperFunction(dialect.formatColumnName(column.getColumn())) : dialect.formatColumnName(column.getColumn());
		String columnValue = new StringBuilder().append("#{").append(parameterName).append("}").toString();
		columnValue = this.isIgnoreCase() ? dialect.upperFunction(columnValue) : columnValue;
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(operator).append(" ").append(getOpen());
		sqlBuilder.append(columnName).append(" ");
		sqlBuilder.append(this.symbol).append(" ").append(columnValue).append(getClose()).append(" ");
		return new StaticTextSqlNode(sqlBuilder.toString());
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.getValue())
			.append(this.getProperty())
			.append(this.getDomainClass())
			.append(this.ignoreCase)
			.append(this.symbol).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(!(obj instanceof CompareCriterion)) {
			return false;
		}
		CompareCriterion criterion = (CompareCriterion) obj;
		return new EqualsBuilder()
				.append(this.getValue(), criterion.getValue())
				.append(this.getProperty(), criterion.getProperty())
				.append(this.getDomainClass(), criterion.getDomainClass())
				.append(this.ignoreCase, criterion.ignoreCase)
				.append(this.symbol, criterion.symbol).isEquals();
	}

	public boolean isIgnoreCase() {
		return ignoreCase;
	}

	public String getSymbol() {
		return symbol;
	}

}
